package logic

import (
	"context"
	"time"
	"xialou_platform/app/errorx"
	"xialou_platform/app/requestx"
	"xialou_platform/app/respx"
	"xialou_platform/app/tools"
	"xialou_platform/services/agent/api/internal/svc"
	"xialou_platform/services/agent/api/internal/types"
	"xialou_platform/services/agent/rpc/agentClient"
	"xialou_platform/services/wallet/rpc/walletClient"

	"github.com/zeromicro/go-zero/core/logx"
)

type AgentUserInfoLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAgentUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) AgentUserInfoLogic {
	return AgentUserInfoLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AgentUserInfoLogic) AgentUserInfo() (resp *types.UserInfoResp, err error) {
	agentId, err := requestx.FetchUserIdByJwt(l.ctx)
	if err != nil {
		return nil, err
	}
	agentParam, err := l.svcCtx.AgentRpc.AgentById(l.ctx, &agentClient.IdBody{Id: agentId})
	if err != nil {
		return nil, errorx.NewMsgCodeError(errorx.UnLoginCode, "请重新登录")
	}
	agentData, err := l.svcCtx.AgentRpc.AgentData(l.ctx, &agentClient.AgentDataReq{
		AgentId: agentId,
		Type:    agentParam.Type,
	})
	var business int64 = 0
	if agentData.StoreNum > 0 {
		business = 1
	}
	//storeCount, err := l.svcCtx.AgentRpc.StoreCount(l.ctx, &agentClient.StoreCountReq{
	//	AgentId:  agentId,
	//	Platform: "xiuxian",
	//	Status:   -1,
	//})
	//if err != nil {
	//	return nil, err
	//}

	//var claimable bool
	var typeNumber string
	var name string
	switch agentParam.Type {
	case 1, 2:
		typeNumber = agentParam.IdCard
		name = agentParam.RealName
	default:
		name = agentParam.Name
	}
	// 已认领数量少于可认领数量时，才允许认领
	//if storeCount.Count < agentParam.StoreNum {
	//	claimable = true
	//}

	storeList, err := l.svcCtx.AgentRpc.StoreList(l.ctx, &agentClient.StoreListReq{
		Page:            1,
		PageSize:        1000,
		Status:          -1,
		Platform:        "xiuxian",
		AgentId:         agentId,
		SelectFirstPass: true,
	})
	if err != nil {
		return nil, err
	}

	ids := make([]string, 0)
	for _, store := range storeList.List {
		ids = append(ids, respx.IDFrom(store.StoreId).ToString())
	}

	authenticated := make([]string, 0)
	if agentParam.Status == 2 {
		authenticated = append(authenticated, "xiuxian")
	}

	var parentType int64
	cardRate := agentParam.CardRate
	if agentParam.ParentUid > 0 {
		parentAgent, err := l.svcCtx.AgentRpc.AgentById(l.ctx, &agentClient.IdBody{Id: agentParam.ParentUid})
		if err != nil {
			return nil, err
		}
		parentType = parentAgent.Type
	}
	if agentParam.TopUid > 0 {
		parentAgent, err := l.svcCtx.AgentRpc.AgentById(l.ctx, &agentClient.IdBody{Id: agentParam.TopUid})
		if err != nil {
			return nil, err
		}
		cardRate = parentAgent.CardRate
	}

	profit, err := l.svcCtx.WalletRpc.AgentProfit(l.ctx, &walletClient.AgentProfitReq{
		AgentId: agentId,
	})
	if err != nil {
		return nil, err
	}

	resp = &types.UserInfoResp{
		Id:         respx.IDFrom(agentId).ToString(),
		Avatar:     agentParam.Avatar,
		Name:       name,
		Gender:     agentParam.Gender,
		Phone:      agentParam.Phone,
		Status:     agentParam.Status,
		Type:       agentParam.Type,
		TypeNumber: typeNumber,
		Claimable:  true,
		Business:   business,
		BindWechat: len(agentParam.OpenId) > 0,
		CreateTime: tools.TimeToString(time.Unix(agentParam.CreateTime, 10), tools.YYYYMMDDHHmmSS),
		AgentData: types.AgentData{
			UserNum:     agentData.UserNum,
			StoreNum:    agentData.StoreNum,
			TotalProfit: profit.CardProfit + profit.GroupProfit + profit.PayProfit,
			TeamSize:    agentData.TeamSize,
			CardProfit:  profit.CardProfit,
			GroupProfit: profit.GroupProfit,
			PayProfit:   profit.PayProfit,
		},
		Authenticated: authenticated,
		ParentUid:     respx.IDFrom(agentParam.Id).ToString(),
		ParentType:    parentType,
		CardRate:      cardRate,
	}
	return resp, nil
}
